package com.esun.cheetah.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.esun.cheetah.R;
import com.esun.cheetah.biz.setting.CheckUpgradeCommunicater;
import com.esun.cheetah.biz.setting.CheckUpgradeCommunicater.UpgradeInfo;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.view.custom.UpgradeDialog;
import com.esun.cheetah.view.news.NewsAct;
import com.esun.cheetah.view.setting.AboutAct;
import com.esun.cheetah.view.setting.ChooseLanguageAct;
import com.esun.cheetah.view.setting.ChooseProTime;
import com.esun.cheetah.view.setting.HelpAct;
import com.esun.cheetah.view.setting.SetDetailsAct;
import com.esun.cheetah.view.setting.UserSuggestAct;
import com.esun.util.VersionUtils;

public abstract class BaseActivity extends Activity implements OnCheckedChangeListener, OnClickListener {
	
	private RadioGroup rgFooter;

	private RadioButton rbMatch;
	private RadioButton rbPeilv;
	private RadioButton buyLottery;
	private RadioButton rbAcount;
	private RadioButton moreRbt;
	private LinearLayout buttomLlyt;
	
	/** 更多动画 */
	private ViewFlipper viewfipper;
	/** 更多弹出popupWindow */
	private PopupWindow popup;

	// 通过标记跳转不同的页面，显示不同的菜单项
	public static int flag = 3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(getLayoutId()) ;
		init() ;
		//footerShow() ;
	}
	
	/**
	 * @param popup
	 * @Title: initMainView
	 * @Description: view初始化
	 * @param
	 * @return
	 * @throws
	 */
	public void init() {
		rgFooter = (RadioGroup) findViewById(R.id.rg_cheetah_footer);
		rgFooter.setOnCheckedChangeListener(this);
		rbMatch = (RadioButton) findViewById(R.id.rb_match);
		rbPeilv = (RadioButton) findViewById(R.id.rbPeilv);
		buyLottery = (RadioButton) findViewById(R.id.rbBuyLottery);
		rbAcount = (RadioButton) findViewById(R.id.rbAccount);
		moreRbt = (RadioButton) findViewById(R.id.moreRbt);
		moreRbt.setOnClickListener(new ShowClickListener());
		buttomLlyt = (LinearLayout) findViewById(R.id.bottomLyyt);
		viewfipper = createViewFilpper();
		LinearLayout llytGrid = createLinearLayout();
		GridView main_gridview = createGridview();
		main_gridview.setAdapter(new MoreGridViewAdapter(new String[] { "新闻", "账户保护", "语言选择", "帮助", "提醒设置", "检查更新", "用户反馈", "关于我们" },
				new int[] { R.drawable.bet, R.drawable.bet, R.drawable.bet, R.drawable.bet, R.drawable.bet, R.drawable.bet, R.drawable.bet,
						R.drawable.bet }));
		main_gridview.setOnItemClickListener(new MoreItemClichistener());
		llytGrid.addView(main_gridview);
		viewfipper.addView(llytGrid);
		popup = createPopupWindow();
	}
	
	/**
	 * 更多弹出的方向
	 * 
	 * @return ViewFlipper
	 */

	private ViewFlipper createViewFilpper() {
		ViewFlipper viewfipper = new ViewFlipper(this);
		viewfipper.setInAnimation(AnimationUtils.loadAnimation(this,
				R.anim.more_in));
		viewfipper.setOutAnimation(AnimationUtils.loadAnimation(this,
				R.anim.more_out));
		viewfipper.setFlipInterval(6000000);
		return viewfipper;
	}

	/**
	 * 更多 弹出文字和图片的方向
	 * 
	 * @return LinearLayout
	 */

	private LinearLayout createLinearLayout() {
		LinearLayout linear_layout = new LinearLayout(this);
		linear_layout.setOrientation(LinearLayout.VERTICAL);
		return linear_layout;
	}

	/**
	 * 更多 GridView
	 * 
	 * @return GridView
	 */
	private GridView createGridview() {
		GridView main_gridview = new GridView(this);
		main_gridview.setSelector(R.drawable.back_selector);
		main_gridview.setLayoutParams(new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		main_gridview.setHorizontalSpacing(1);
		main_gridview.setVerticalSpacing(5);
		main_gridview.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
		main_gridview.setGravity(Gravity.CENTER);
		main_gridview.setNumColumns(4);
		return main_gridview;
	}

	/**
	 * more adapter
	 * 
	 * @author renfj
	 * 
	 */

	class MoreGridViewAdapter extends BaseAdapter {
		@Override
		public boolean areAllItemsEnabled() {
			return super.areAllItemsEnabled();
		}

		private String[] strs;
		private int[] images;

		public MoreGridViewAdapter(String[] strs, int[] images) {
			this.strs = strs;
			this.images = images;
		}

		@Override
		public int getCount() {
			return strs.length;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
		
			return null;
		}
	}

	/**
	 * 弹出popupWindow
	 * 
	 * @return
	 */

	private PopupWindow createPopupWindow() {
		PopupWindow popup = new PopupWindow(viewfipper,
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		popup.setFocusable(true);
		popup.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.bg_default));
		popup.update();
		return popup;
	}

	/**
	 * popupWindow 里面每一项的点击事件
	 * 
	 * @author renfj
	 * 
	 */

	class MoreItemClichistener implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			onItemChance(position);
		}

	}

	/**
	 * 点击 弹出 popupWindow 的弹出或者关闭事件
	 * 
	 * @author renfj
	 * 
	 */
	class ShowClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			if (popup != null) {
				if (popup.isShowing()) {
					popup.dismiss();
				} else {
					popup.showAtLocation(buttomLlyt, Gravity.BOTTOM, 0, buttomLlyt.getHeight() + 10);
					popup.showAtLocation(buttomLlyt, Gravity.BOTTOM, 0,
							buttomLlyt.getHeight() + 10);
					viewfipper.startFlipping();
				}
			}

		}

	}

	/**
	 * 更多每一项的 跳转
	 * 
	 * @param position
	 */
	private void onItemChance(int position) {
		Intent mIntent = new Intent();
		switch (position) {
		case 0:
			mIntent.setClass(BaseActivity.this, NewsAct.class);
			startActivity(mIntent);
			break;
		case 1:
			mIntent.setClass(BaseActivity.this, ChooseProTime.class);
			startActivity(mIntent);
			break;
		case 2:
			mIntent.setClass(BaseActivity.this, ChooseLanguageAct.class);
			startActivity(mIntent);
			break;
		case 3:
			mIntent.setClass(BaseActivity.this, HelpAct.class);
			startActivity(mIntent);
			break;
		case 4:
			mIntent.setClass(BaseActivity.this, SetDetailsAct.class);
			startActivity(mIntent);
			break;
		case 5:
			checkUpgrade();
			popup.dismiss();
			break;
		case 6:
			mIntent.setClass(BaseActivity.this, UserSuggestAct.class);
			startActivity(mIntent);
			break;
		case 7:
			mIntent.setClass(BaseActivity.this, AboutAct.class);
			startActivity(mIntent);
			break;
		default:
			break;
		}
	}
	
	/**
	 * @title checkUpgrade
	 * @description 检查更新
	 */
	private void checkUpgrade() {
		Toast.makeText(BaseActivity.this, "正在检测新版本...", Toast.LENGTH_LONG).show();
		Handler handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				if (msg.what == Config.COMMUNICATER_SUCCESS) {
					VersionUtils.getInstance().setActived(BaseActivity.this);
					UpgradeInfo info = (UpgradeInfo) msg.obj;
					if (info.isUpgrade) {
						showUpgradeDialog(info);
					} else {
						showNotNeedUpgrade();
					}
				} else {
					Toast.makeText(BaseActivity.this.getParent(), "检测失败！", Toast.LENGTH_SHORT).show();
				}
			}

		};
		final String channel = VersionUtils.getInstance().getChannel(this);
		final int version = VersionUtils.getInstance().getVersion(this);
		final int isJiHuo = VersionUtils.getInstance().getIsActive(this);
		final String sysVersion = Build.VERSION.RELEASE;
		final String screenreso = VersionUtils.getInstance()
				.getScreenreso(this);
		final String serial = VersionUtils.getInstance().getSerial(this);
		if (TextUtils.isEmpty(channel) || version < 0) {
			return;
		}
		CheckUpgradeCommunicater mCheckUpgradeCommunicater = new CheckUpgradeCommunicater(handler, this, version, channel, sysVersion,
				screenreso, serial, isJiHuo);
		mCheckUpgradeCommunicater.startCommunicate();
	}
	
	/**
	 * @title showUpgradDialog
	 * @description 显示下载的对话框
	 * @param info
	 */
	protected void showUpgradeDialog(final UpgradeInfo info) {
		final UpgradeDialog dialog = new UpgradeDialog(this);
		dialog.setMessage(Html.fromHtml(info.upgradeDescription));
		dialog.setOnLeftButtonClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.setOnRightButtonClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				Intent downloadIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(info.url));
				startActivity(downloadIntent);
			}
		});
		dialog.show();
	}

	/**
	 * @title showNotNeedUpgrade
	 * @description
	 */
	protected void showNotNeedUpgrade() {

		final Dialog showVersionUpdate = new Dialog(this, R.style.dialog);
		showVersionUpdate.getWindow().setContentView(R.layout.version_update);
		showVersionUpdate.show();
		Button btnSure=(Button) showVersionUpdate.findViewById(R.id.btUserSure);
		btnSure.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showVersionUpdate.cancel();
				
			}
		});
	}
	
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {


		switch (checkedId) {

		// 比赛点击事件
		case R.id.rb_match:
			flag = 0;

			break;

		// 赔率
		case R.id.rbPeilv:
			flag = 1;

			break;

		// 购彩
		case R.id.rbBuyLottery:
			flag = 2;

			break;

		// 账户
		case R.id.rbAccount:
			flag = 3;

			break;

		default:
			break;
		}

		showView(flag);
	}
	
	/**
	 * @Title: showView
	 * @Description: 控制加载不同的页面
	 * @param flag
	 * @return
	 * @throws
	 */
	public void showView(int flag) {
		
		switch (flag) {
		case 0:
			// 加载比赛列表页面
			intentJumper(BaseActivity.this, 0) ;
			break;

		case 1:
			intentJumper(BaseActivity.this, 1) ;
			
			break;

		case 2:
			intentJumper(BaseActivity.this, 2) ;
			
			break;

		case 3:
			intentJumper(BaseActivity.this, 3) ;

			break;
		case 4:
			intentJumper(BaseActivity.this, 4) ;

			break;
		default:
			break;
		}
	} 
	
	public void intentJumper (Context context, int flag) {
		Intent intent = new Intent() ;
		intent.setClass(context, HomeAct.class);
		HomeAct.flag = flag;
		startActivity(intent);
		this.finish() ;
	}
	
	public void footerShow() {
		switch (flag) {
		case 0:
			rbMatch.setCompoundDrawables(null, getResources().getDrawable(R.drawable.icon_focused_bettingrecord_menu_match_48x32), null, null);
			break;
		case 1:
			rbPeilv.setChecked(true);
			break;
		case 2:
			buyLottery.setChecked(true);
			break;
		case 3:
			rbAcount.setCompoundDrawables(null, getResources().getDrawable(R.drawable.icon_focused_bettingrecord_menu_match_48x32), null, null);
			break;
		default:
			break;
		}
	}

	abstract protected int getLayoutId() ;

}
